package com.lance.answer.gui.answer.gk.answer;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.answer.task.DoEngAnswerTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

// 批量做英语题 FIXME 2022/6/1 还有一些英语url没加入
@Slf4j
public class BatchDoEngAnswertmppppppppppppp {

    public static void main(String[] args) throws Exception {

        Thread.sleep(1000*60*60*1);
    //    Thread.sleep(2*60*60*1000);
        int sync = 14;

       // String teacher = "刘庆";
    //    String teacher = "李明";
      //  String teacher = "刘";
       // String teacher = "李俊杰";
    //    String teacher = "艾";

        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李明");
        teachers.add("刘");
        teachers.add("李俊杰");
        teachers.add("艾");

        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        Set<String> names = Sets.newHashSet();
       for(File file:new File("D:\\newAnswer\\deal1014").listFiles()){
           InputStream answerInputStream = new FileInputStream(file);
           String answerStr = IOUtils.toString(answerInputStream,"UTF-8");
           List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
           Map<String, AnswerObject> answers = new HashMap<>();
           Map<String, AnswerObject> originAnswers = new HashMap<>();
           for (AnswerObject answerObject : list) {
               answers.put(answerObject.getQuestionCode(), answerObject);
//               originAnswers.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()),answerObject);
               originAnswers.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()),answerObject);
               originAnswers.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()), answerObject);
               originAnswers.put(Util.dealContent(answerObject.getQuestionTitle()), answerObject);
               originAnswers.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
               if(answerObject.getAnswers()==null||answerObject.getAnswers().isEmpty()){
                   System.err.println(file.getName()+"\t"+answerObject.getQuestionCode());
                   names.add(file.getName());
               }
           }
       }


        for(String teacher:teachers){

            for(String n:names){
                String subjectName = n.substring(0,n.length()-12);
                //    System.err.println("subjectName:"+subjectName);
                List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+teacher+"//").get(subjectName);
                if(userNames !=null&&userNames.size()>0){
                    System.err.println(n);
                }
            }







            // 获取所有的用户名和密码
            // InputStream userInputStream = new FileInputStream("libs//allUser.txt");
            InputStream userInputStream = new FileInputStream("libs//teachers//"+teacher+".user");
            List<String> userString = IOUtils.readLines(userInputStream, "utf-8");
            Map<String, String> usernameAndPwd = Maps.newHashMap();
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = "";
                if(strs[1].length()<18){
                    pwd = strs[1];
                }else {
                    pwd = strs[1].substring(6, 14);
                    pwd = "Ouchn" + pwd;
                }
                usernameAndPwd.put(userCode, pwd);
            }


            // key = subjectName value 做题链接集合
            Map<String,  Set<String> > mapBySubjectName = Maps.newHashMap();
            for(File f1: Objects.requireNonNull(new File("libs//courseUrlInfo").listFiles())){
                Set<String> urlTaskSet = Sets.newHashSet();
                if(f1.length()<100){
                    continue;
                }
                String fileName = f1.getName();
                String subjectName = fileName.substring(0,fileName.length()-4);
                List<String> urlInfoList = IOUtils.readLines(new FileInputStream(f1), "utf-8");
                for(String urlInfo:urlInfoList){
                    if(urlInfo.contains("quiz")){
                        if(urlInfo.contains("形考")
                                ||urlInfo.contains("作业")
                                ||urlInfo.contains("专题测验")
                                ||urlInfo.contains("单元自测")
                                ||urlInfo.contains("终考任务")
                                ||urlInfo.contains("Self-test")
                        ){
                            String urlTask = urlInfo.split(",")[0];
                            urlTaskSet.add(urlTask);
                        }
                    }

                }
                mapBySubjectName.put(subjectName,urlTaskSet);
            }

//        for(String kk:mapBySubjectName.keySet()){
//            if(kk.equals("建筑工程项目管理")){
//                System.err.println("22222222222222222222222222222222222222222222222222222222222222222");
//                System.out.println(mapBySubjectName.get(kk));
//            }
//        }




            for(String subjectName:mapBySubjectName.keySet()){

                // 只要英语
                if(Strings.isNullOrEmpty(subjectName)||!subjectName.contains("英语")){
                    continue;
                }
                if(mapBySubjectName.get(subjectName).size()<1){
                    continue;
                }
                log.info("subjectName:{}",subjectName);

                //
                // D:\newAnswer\deal1014
                File answerFile = new File("D:\\project\\answer-tool\\src\\main\\resources\\"+subjectName + "\\" + subjectName + "-answer.json");
                if(!answerFile.exists()){
                    System.err.println(">>>>>>>>>>:"+subjectName);
                    answerFile = new File("D:\\newAnswer\\deal1014\\"+ subjectName + "-answer.json");
                }
                if(!answerFile.exists()){
                    System.err.println("??????????????11111111111111:"+subjectName);
                    log.info("{}需要拿到题目答案",subjectName);
                    continue;
                }



                InputStream answerInputStream = new FileInputStream(answerFile);
                String answerStr = IOUtils.toString(answerInputStream,"UTF-8");
                List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
                Map<String, AnswerObject> answers = new HashMap<>();
                Map<String, AnswerObject> originAnswers = new HashMap<>();
                boolean hasEmptyAnswer = false;
                for (AnswerObject answerObject : list) {
                    answers.put(answerObject.getQuestionCode(), answerObject);

                    originAnswers.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()),answerObject);
                    originAnswers.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()),answerObject);

                    if(answerObject.getAnswers()==null||answerObject.getAnswers().isEmpty()){
                        if(DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+teacher).get(subjectName)!=null){
                            System.err.println("????????????????+"+subjectName+"\t" +answerObject.getQuestionCode()+"\t"+DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+teacher).get(subjectName).size());
                        }
                        hasEmptyAnswer = true;
                        break;
                    }
                }
                if(hasEmptyAnswer){
                    continue;
                }



                List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+teacher).get(subjectName);
                if(userNames == null){
                    continue;
                }

                for(String username:userNames){

//               if(!Util.getVip().contains(username)){
//                   continue;
//               }
//               if(!"2136001453127".equals(username)){
//                   continue;
//               }


                    String pwd = usernameAndPwd.get(username);
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSubjectName(subjectName);
                    answerDTO.setUsername(username);
                    answerDTO.setPwd(pwd);
                    answerDTO.setAnswers(answers);
                    answerDTO.setOriginAnswers(originAnswers);
                    answerDTO.setCommit(true);
                    answerDTO.setPush(false);
                    answerDTO.setAnswerUrl(Lists.newLinkedList(mapBySubjectName.get(subjectName)));
                    answerDTO.setTeacher(teacher);
                    //     System.err.println("answerDTO:"+answerDTO);
                    // FIXME 过滤掉英语
                    if (!subjectName.contains("英语")){
                        continue;
                    }
                    answerQueue.add(answerDTO);
                }

            }
        }



   //    Thread.sleep(20000000);




        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoEngAnswerTask(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }




}
